<?php
//---------------------------------------------------------------------------------------------------
    require_once('../../config.inc.php');

    require_once($config['app_dir'] . '/includes/date.class.php');
    require_once($config['app_dir'] . '/classes/sparklines.deamon.php');
    require_once($config['app_dir'] . '/classes/forecasts.deamon.php');
    require_once($config['app_dir'] . '/classes/StockMailers.class.php');
    //-----------------------------------------------------------------------------------------------
    set_time_limit(0);
    $user = false;
    $password = false;

    if( 'cli'==php_sapi_name() ) { //console access
        $trade = intval($argv[1]);
        $start = intval($argv[2]);
        $finish = intval($argv[3]);
        $system = intval($argv[4]);
    } else { //web access
        header('Content-Type: text/html; charset=utf-8');
        $user = strval($_GET['user']);
        $password = strval($_GET['password']);
        if( $config['manual']['user']!=$user || md5($config['manual']['password'])!=md5($password) ) die('user not set');
        $StockParams = new objUStockParams(true, false);

        $trade = "2400103";
        $start = 1201003775;
        $finish = 1203291200;
        $system= 2;
    }//

    if( !$finish && $start ) {
        $finish  = $start + 60*60*24;
        while( !DateConvert::isWeekendDay($finish) ) $finish  += 60*60*24;
    }//if finish not set

    if( !$finish ) die('Period set not correctly');
/*
    echo "<hr>";
    echo 'Trade';
    var_dump($trade);
    echo 'Start';
    var_dump($start);
    echo 'Finish';
    var_dump($finish);
    echo 'System';
    var_dump($system);
    echo "<hr>";
*/

    try {
       	$Mailer = new objExportServiceProcess($trade, $start, 1, $system);
       	$Mailer->Send();
    } catch(exception $e) {
        die('Error: '.$e->getMessage());
    }
    //-----------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------
    try {
        $start = $finish - 60*60*24*8; //show 9 days - 2 days of weekend
       	$StockMailer = new objStockParamMailer($trade,
			DateConvert::Date('Y-m-d', $start),
			DateConvert::Date('Y-m-d', $finish), -1, $system);
       	$StockMailer->Send();
    } catch(exception $e) {
        die('Error: '.$e->getMessage());
    }
    //-----------------------------------------------------------------------------------------------
    $LastForecastDeamon = new objLastForecastDeamon();
    $LastForecastDeamon->Render();
    //-----------------------------------------------------------------------------------------------
    $LastSparklineInDeamon = new objLastSparklineInDeamon();
    $LastSparklineInDeamon->Render();
    //-----------------------------------------------------------------------------------------------
//    $LastSparklineOutDeamon = new objLastSparklineOutDeamon();
//    $LastSparklineOutDeamon->Render();
    //-----------------------------------------------------------------------------------------------
    echo "<br>Done<br>";
//---------------------------------------------------------------------------------------------------
?>
